package com.yzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yzx.constants.Constants;
import com.yzx.dto.SimpleUser;
import dto.SysMenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzx.mapper.SysMenuMapper;
import com.yzx.domain.SysMenu;
import com.yzx.service.SysMenuService;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     *
     * @param isAdmin 是否是超级管理员
     * @param  simpleUser  如果isAdmin=true  simpleUser可以为空
     * @return
     */
    @Override
    public List<SysMenu> selectMenuTree(boolean isAdmin, SimpleUser simpleUser) {
        QueryWrapper<SysMenu> qw=new QueryWrapper<>();
        qw.eq(SysMenu.COL_STATUS, Constants.STATUS_TRUE);
        qw.in(SysMenu.COL_MENU_TYPE, Constants.MENU_TYPE_M,Constants.MENU_TYPE_C);
        qw.orderByAsc(SysMenu.COL_PARENT_ID);
        if(isAdmin){
            return sysMenuMapper.selectList(qw);
        }else{
            //根据用户id查询用户拥有的菜单信息
            return sysMenuMapper.selectList(qw);
        }
    }

    @Override
    public List<Map<String, Object>> dynamicMenu() {
        List<Map<String, Object>> list = new ArrayList<>();
        QueryWrapper<SysMenu> qw=new QueryWrapper<>();
        qw.eq(SysMenu.COL_STATUS, Constants.STATUS_TRUE);
        qw.in(SysMenu.COL_MENU_TYPE, Constants.MENU_TYPE_M,Constants.MENU_TYPE_C);
        qw.orderByAsc(SysMenu.COL_PARENT_ID);
        List<SysMenu> sysMenus = sysMenuMapper.selectList(qw);
        for (SysMenu sysMenu : sysMenus) {
            //拼接父菜单
            if (sysMenu.getParentId()==0){
                Map<String, Object> map = new HashMap<>();
                map.put("path",sysMenu.getPath());
                map.put("component","Layout");
                map.put("redirect","noRedirect");
                map.put("alwaysShow",true);
                map.put("name",sysMenu.getPath());
                //拼接图标
                Map<String, Object> icon = new HashMap<>();
                icon.put("title",sysMenu.getMenuName());
                icon.put("icon",sysMenu.getIcon());
                map.put("meta",icon);
                //查询出父菜单的儿子菜单
                List<Map<String, Object>> chiledList = new ArrayList<>();
                List<SysMenu> menuByParentId = this.getMenuByParentId(sysMenu.getMenuId());
                //组装开始
                for (SysMenu menu : menuByParentId) {
                    Map<String, Object> childMap = new HashMap<>();
                    childMap.put("path",menu.getPath());
//                    childMap.put("component"," () => import('"+menu.getPath()+"')");
                    childMap.put("component",menu.getPath());
                    childMap.put("name",menu.getPath());
                    Map<String, Object> childIcon = new HashMap<>();
                    //儿子图标
                    childIcon.put("title",menu.getMenuName());
                    childIcon.put("icon",menu.getIcon());
                    childMap.put("meta",childIcon);
                    chiledList.add(childMap);

                }
                map.put("children",chiledList);
                list.add(map);
            }

        }
        return list;
    }

    @Override
    public List<SysMenu> getMenuByParentId(Long id) {
        QueryWrapper<SysMenu> qw=new QueryWrapper<>();
        qw.eq(SysMenu.COL_STATUS, Constants.STATUS_TRUE);
        qw.in(SysMenu.COL_MENU_TYPE, Constants.MENU_TYPE_M,Constants.MENU_TYPE_C);
        qw.eq(SysMenu.COL_PARENT_ID,id);
        return sysMenuMapper.selectList(qw);
    }

    @Override
    public List<SysMenu> listAllMenus(SysMenuDto sysMenuDto) {
        QueryWrapper<SysMenu> qw=new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(sysMenuDto.getMenuName()),SysMenu.COL_MENU_NAME, sysMenuDto.getMenuName());
        qw.eq(StringUtils.isNotBlank(sysMenuDto.getStatus()),SysMenu.COL_STATUS,sysMenuDto.getStatus());
        return sysMenuMapper.selectList(qw);
    }

    @Override
    public int addMenu(SysMenuDto sysMenuDto) {
        SysMenu sysMenu = new SysMenu();
        BeanUtil.copyProperties(sysMenuDto,sysMenu);
        //设置创建人和创建时间
        sysMenu.setCreateBy(sysMenuDto.getSimpleUser().getUserName());
        sysMenu.setCreateTime(DateUtil.date());
        return sysMenuMapper.insert(sysMenu);
    }

    @Override
    public int updateMenu(SysMenuDto sysMenuDto) {
        SysMenu sysMenu = new SysMenu();
        BeanUtil.copyProperties(sysMenuDto,sysMenu);
        //设置修改人和修改时间
        sysMenu.setUpdateBy(sysMenuDto.getSimpleUser().getUserName());
        sysMenu.setUpdateTime(DateUtil.date());
        return sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public int deleteMenuById(Long menuId) {
        return sysMenuMapper.deleteById(menuId);
    }

    @Override
    public SysMenu getMenuById(Long menuId) {
//        Wrapper<SysMenu> wrapper=new QueryWrapper<>();
//        wrapper
        return sysMenuMapper.selectById(menuId);
    }

    @Override
    public boolean hasChildMenu(Long menuId) {
        return sysMenuMapper.hasChildMenu(menuId)>0 ? true : false;
    }
}

